﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using Skynet.WebFoundation.Services.Session;
using Skynet.WebFoundation.Services.Navigation;
using Skynet.BusinessEntities;

namespace Skynet.WebAdministracion.Views.DesignAlternative
{
    public class DesignAlternativePresenter : Infrastructure.Web.Views.Master.MasterPresenter<IDesignAlternativeView>
    {
        [Dependency]
        public BusinessLogic.BusinessComponents.IBCExams BCExams { get; set; }

        [Dependency]
        public BusinessLogic.BusinessComponents.IBCExamQueries BCExamQueries { get; set; }

        [Dependency]
        public INavigationService NavigationService { get; set; }



        [Dependency]
        public ISessionService SessionService { get; set; }

        public override void OnViewInitialized()
        {
            BusinessEntities.BaseAlternative alternative = null;

            var question = this.BCExamQueries.SearchQuestion(options: BusinessLogic.BusinessComponents.QuestionSearchOptions.DesignOneWithAlternatives, QuestionId: this.SessionService.DesignQuestionId).Single();
            this.View.QuestionTitle = question.Title;

            if (this.SessionService.DesignAlternativeId != null)
            {
                alternative = this.BCExamQueries.SearchAlternatives(options: BusinessLogic.BusinessComponents.SearchAlternativesOptions.DesignOne, AlternativeId: this.SessionService.DesignAlternativeId).Single();
            }
            else
            {
                alternative = new BusinessEntities.BoolAlternative();
                alternative.Id = Guid.NewGuid();
                alternative.QuestionId = this.SessionService.DesignQuestionId.Value;
                alternative.Score = 0;                
                alternative.State = BusinessEntities.ObjectState.Added;
                ((BusinessEntities.BoolAlternative)alternative).Answer = true;
                ((BusinessEntities.BoolAlternative)alternative).Data = true;
                this.BCExams.MaintenanceAlternative(options: BusinessLogic.BusinessComponents.MaintenanceAlternativeOptions.CUD, alternative: alternative);
                this.SessionService.DesignAlternativeId = alternative.Id;
            }
            this.View.Score = alternative.Score;

            if (alternative is BoolAlternative)
            {
                var target = alternative as BoolAlternative;
                var control = new WebFoundation.Controls.Design.BoolAlternativeControl();
                control.Answer = target.Answer;
                this.View.AlternativeControl = control;
            }
        }
        public override void OnViewLoaded()
        {
            var alternative = this.BCExamQueries.SearchAlternatives(options: BusinessLogic.BusinessComponents.SearchAlternativesOptions.DesignOne, AlternativeId: this.SessionService.DesignAlternativeId).Single();
            if (alternative is BoolAlternative)
            {
                var target = alternative as BoolAlternative;
                var control = new WebFoundation.Controls.Design.BoolAlternativeControl();                
                this.View.AlternativeControl = control;
            }
        }

        private void UpdateAlternativeInformation()
        {
            var alternative = this.BCExamQueries.SearchAlternatives(options: BusinessLogic.BusinessComponents.SearchAlternativesOptions.DesignOne, AlternativeId: this.SessionService.DesignAlternativeId).Single();
            alternative.Score = this.View.Score;
            if (alternative is BoolAlternative)
            {
                var target = alternative as BoolAlternative;
                var control = this.View.AlternativeControl as WebFoundation.Controls.Design.BoolAlternativeControl;
                target.Answer = control.Answer;
            }
            alternative.State = BusinessEntities.ObjectState.Modified;
            this.BCExams.MaintenanceAlternative(options: BusinessLogic.BusinessComponents.MaintenanceAlternativeOptions.CUD, alternative: alternative);
 
        }
        public void BackQuestions()
        {
            UpdateAlternativeInformation();
            this.NavigationService.NavigateRoute(Constants.RoutesNames.DesignQuestion);
        }
        public void NewAlternative()
        {
            UpdateAlternativeInformation();
            this.NavigationService.NavigateRoute(Constants.RoutesNames.DesignAlternative);
        }
    }
}
